package com.controller;

import com.entity.ShopNews;
import com.service.ShopNewsService;
import com.util.PageTool;
import com.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.util.Date;
import java.util.List;

@Controller
public class ShopNewsController {

    @Autowired
    private ShopNewsService shopNewsService;

    @RequestMapping("shopNewsPage")
    public String shopNewsPage(String currentPageNo, Model model) {
        PageTool pageTool = new PageTool();
        pageTool.setCurrentPageNo(currentPageNo);
        int count = shopNewsService.getCountNews();
        pageTool.setTotalCount(count);
        List<ShopNews> newsPage = shopNewsService.getAllNewsByPage(pageTool.getCurrentPageNo(), pageTool.getPageSize());
        model.addAttribute("newsList", newsPage);
        model.addAttribute("pageTool", pageTool);
        return "shopNews";
    }

    @RequestMapping(value = "updateNews", method = RequestMethod.GET)
    public String updateBank(HttpServletRequest request, Model model) throws ParseException {
        int id = Integer.parseInt(request.getParameter("id"));
        System.out.println("++++++" + id + "==========");
        String title = request.getParameter("title");
        String content = request.getParameter("content");
        Date createTime = new Date(request.getParameter("createTime"));
        String picName = request.getParameter("picName") == null ? null : request.getParameter("picName");
        String imgName = request.getParameter("imgName") == null ? null : request.getParameter("imgName");
        ShopNews news = new ShopNews(id, title, content, createTime, picName, imgName);
        model.addAttribute("news", news);
        return "updateNews";
    }

    @RequestMapping(value = "updateNews", method = RequestMethod.POST)
    public String updateBank(ShopNews news) {
        //news.setId(id);
        System.err.println(news);
        int i = shopNewsService.updateByPrimaryKeySelective(news);
        System.err.println(i + "+++++");
        return "redirect:shopNewsPage";
    }

    @RequestMapping("removeMoreNews")
    public String removeMoreNews(Integer[] ids, HttpServletResponse response) throws IOException {
        int i = shopNewsService.removeMoreNews(ids);
        System.out.println("成功删除" + i + "条记录");
        JSMessage(response, "批量删除");
        return "redirect:shopNewsPage";
    }

    //Ajax
    @ResponseBody
    @RequestMapping("removeNewsById")
    public Result bankList(Integer id) {
        //调用业务逻辑层
        int num = shopNewsService.deleteByPrimaryKey(id);
        Result result = new Result();
        if (num > 0) {
            result.setCode(200);
        } else {
            result.setFlag(false);
        }
        result.setFlag(true);
        result.setMessage("删除id为"+id+"的新闻成功");

        return result;
    }


    @RequestMapping("addNews")
    public void saveBank(ShopNews news, HttpServletResponse response) throws IOException {
        shopNewsService.addNews(news);
        JSMessage(response, "添加");
    }

    @ResponseBody
    @GetMapping("getAllNews")
    public Result getAllNews() {
        List<ShopNews> allNews = shopNewsService.getAllNews();
        Result result = new Result();
        result.setData(allNews);
        result.setCode(200);
        result.setFlag(true);
        result.setMessage("得到所有新闻成功");

        return result;

    }


    private void JSMessage(HttpServletResponse response, String mes) throws IOException {
        response.setContentType("text/html;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        PrintWriter out = response.getWriter();
        out.write("<script>alert('恭喜你," + mes + "新闻成功！');location.href='shopNewsPage';</script>");
    }

}
